package cn.coastalinfo.project.drools.utils;

import cn.coastalinfo.common.utils.StringUtils;
import cn.coastalinfo.project.drools.domain.Rule;
import cn.coastalinfo.project.drools.domain.RuleCondition;
import cn.coastalinfo.project.drools.enums.ConditionType;
import cn.coastalinfo.project.drools.enums.LeftAssignEnums;

public class DroolsRuleUtils {
    public static String rule2Drl(Rule rule,String condition) {
        StringBuilder result = new StringBuilder();
        /*package部分*/
        result.append("package  cn.coastalinfo.drools.rule888;\r\n");
        result.append("\r\n");

        /*导包部分*/
        result.append("import cn.coastalinfo.project.drools.fact.RuleFact;\r\n");
        result.append("\r\n");

        /*规则申明部分*/
//        result.append("rule \"3333\"\r\n");
        result.append(String.format("rule \"%s\"\r\n", rule.getName()+System.currentTimeMillis()));

        /*规则属性部分*/

        /*规则条件部分*/
        result.append("\twhen\r\n");
        result.append("\t\t$result:RuleFact()\r\n");
        result.append(condition);

        /*规则结果部分*/
        result.append("\tthen\r\n");
        result.append("\t\tSystem.out.println(\"%s规则被触发了\");\r\n");
        result.append(String.format("\t\tSystem.out.println(\"%s被触发了\");\r\n",rule.getName()));
        result.append(String.format("\t\t$result.add(\"%s\");\r\n", rule.getThenSet()));

        /*规则结束*/
        result.append("end\r\n");
        return result.toString();
    }

    /**
     * 获取条件或普通规则
     * @param rc
     * @return
     */
    public static String contion2Str(RuleCondition rc) {
        if(rc.getType() == ConditionType.AND || rc.getType() == ConditionType.OR){
            if(rc.getChildren() == null || rc.getChildren().size()<1){
                return "";
            }
            return ConditionType.AND.getCode();
        }
        if(rc.getType() == ConditionType.NORMAL){
            //运算式左值右值
            String left = "1";String right = "1";
            if(rc.getLeftType() == LeftAssignEnums.Input.getCode()){
                if(">=<==".contains(rc.getOperator()) && rc.getLeftValue().matches("-?\\d+(\\.\\d+)?")){//保留数字
                    left = rc.getLeftValue();
                }else if("not contains".contains(rc.getOperator())){
                    left = String.format("getStrList(map[\"%s\"])",rc.getLeftValue());
                }else{
                    left = String.format("\"%s\"",rc.getLeftValue());
                }
            }
            if(rc.getLeftType() == LeftAssignEnums.Variable.getCode()){
                left = String.format("map[\"%s\"]",rc.getLeftValue());
            }
            //右值
            if(rc.getRightType() == LeftAssignEnums.Input.getCode()){
                if(">=<==".contains(rc.getOperator()) && rc.getRightValue().matches("-?\\d+(\\.\\d+)?")){//保留数字
                    right = rc.getRightValue();
                }else if("not member of".contains(rc.getOperator())){
                    right = String.format("\"%s\"",rc.getRightValue());
                }else{
                    right = String.format("\"%s\"",rc.getRightValue());
                }
            }
            if(rc.getLeftType() == LeftAssignEnums.Variable.getCode()){
                right = String.format("map[\"%s\"]",rc.getRightValue());
            }
            return String.format("RuleFact(%s %s %s)\r\n", left,rc.getOperator(),right);
        }
        return "";
    }

    /**
     * 获取LHS
     * @param rc
     * @return
     */
    public static String contion2LHS(RuleCondition rc,StringBuilder str,int num) {
        Boolean isRoot = rc.getType()!= ConditionType.NORMAL;
        String s = contion2Str(rc);//条件
        if(StringUtils.isNotBlank(s)){
            String sk = "";
            if(isRoot && rc.getChildren() != null && rc.getChildren().size()>1){
                sk = "(";
            }
            str.append("\t\t"+sk+getKong(num)+s);
            str.append("\r\n");
        }
        if(isRoot){
            num+=1;
        }
        if (rc.getChildren() != null) {
            for (RuleCondition child : rc.getChildren()) {
                contion2LHS(child,str,num); // 递归插入孙节点，以此类推
            }
        }
        if(isRoot && StringUtils.isNotBlank(s) && rc.getChildren() != null && rc.getChildren().size()>1){
            str.append(")\r\n");
        }
        return str.toString();
    }

    public static String getKong(int num) {
        String str = "";
        for(int i = 0; i < num; i++){
            str +="\t";
        }
        return str;
    }
}
